/*
 * (C) 2012 Sheng Yi
 * This program selects the special charators in string, then sort
 * Those sepcial charctors are : a e i o u A E I O U
 * And lower case letters should be front of uppercase letters
 */
#include <stdio.h>
#include <string.h>

int  Vowel[26 * 2 + 6] = {
	// from A ~ Z;  1 --- special char, 0 --- normal
	1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
	// uppercase + 0x32 = lowercase
	0, 0, 0, 0, 0, 0,
	// from a ~ z;  1 --- special char, 0 --- normal
	1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
};


void sortVowel(char* input, char* output);

int main()
{
	char* input = "Abort!May Be Some Errors In Our System.";
	char output[100];
	sortVowel(input, output);
	printf("sorted vowel array are: %s\n", output);
	return 0;
}


void sortVowel(char* input, char* output)
{
	int len = strlen(input);
	int spilit = 0, pos = 0;
	int i, j;
	for (i = 0; i < len; i++)
	{
		// not letters, just ignore
		if (input[i] < 'A' || input[i] > 'z')
			continue;
		int letter = input[i] - 'A';
		if (Vowel[letter] == 1) // vowel letter
		{
			pos += 1;
			if (letter < 26) // uppercase
			{
				j = pos - 2;
				while (j >= spilit)
				{
				       if (input[i] < output[j])
				       {
					       output[j + 1] = output[j];
					       j--;
				       }
				       else
					       break;
				}
				output[j + 1] = input[i];
			}
			else // lower case
			{
				// first, move back the uppercase
				j = pos - 2;
				while (j >= spilit)
				{
					output[j + 1] = output[j];
					j--;
				}

				// then, inset the lowercase
				while (j >= 0)
				{
					if (input[i] < output[j])
					{
						output[j + 1] = output[j];
						j--;
					}
					else
						break;
				}
				output[j + 1] = input[i];
				spilit += 1;
			}
		}
	}
	output[pos] = '\0';
}
